package com.coreJava.test01;

import java.io.IOException;
import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * 作者: LCG
 * 日期: 2019/9/1 11:21
 * 描述:
 */
public class Test0101 {
    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {


        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> get(10, "A"))
                .thenCombineAsync(CompletableFuture.supplyAsync(() -> get(5, "B"))
                        , (s1, s2) -> s1 + s2);


        stringCompletableFuture.thenAccept((x)->{
            System.out.println("============>"+x);
        });

        System.in.read();



    }

    public static String get(int time,String name){
        FutureTask<String> stringFutureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                if(time==8){
                    throw new RuntimeException("任务运行出现错误");
                }
                System.out.println("=====任务====="+Thread.currentThread().getName());
                Thread.sleep(time*1000);
                return "====当前执行的线程=>"+name;
            }
        });
        Thread thread=new Thread(stringFutureTask,name);
        thread.start();

        try {
            return stringFutureTask.get();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
